package database_interaction;

/**
 * @file InsertQuery.java
 * @version 0.1
 * @author James Dibble 10009689
 */
public class InsertQuery extends SQLQuery{
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructors">
    public InsertQuery(String table) throws JDBCException {
        super(QueryType.INSERT, table);
    }
    
    public InsertQuery(String table, ParameterList parms) throws JDBCException {
        super(QueryType.INSERT, table);
        super._parms = parms;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Methods">
    @Override
    public String getQueryString() throws JDBCException{
        if(super._parms.getColumns().size() != super._parms.getColumns().size())
            throw new JDBCException("The number of columns does not match the number of parameters.");
        String strValues = "";
        String strColumns = "";
        for(Parameter thisParm: super._parms.getParms()){
            if(thisParm instanceof NVarChar){
                strValues += "'" + thisParm.value() + "',";
            }
            else{
                strValues += thisParm.value() + ",";
            }
        }
        strValues = strValues.substring(0, strValues.length() - 1);
        
        for(String thisCol: super._parms.getColumns()){
            strColumns += thisCol + ",";
        }
        strColumns = strColumns.substring(0, strColumns.length() - 1);
        
        return String.format("INSERT INTO %s (%s) VALUES(%s)", 
                             super.getTable(),
                             strColumns,
                             strValues);
    }
    // </editor-fold>
}